package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yenlien.traceyun.business.dao.ClientDao;
import com.yenlien.traceyun.business.dao.SupplierDao;
import com.yenlien.traceyun.business.entity.Client;
import com.yenlien.traceyun.business.entity.Supplier;
import com.yenlien.traceyun.business.entity.UserBusiness;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.UserService;
import com.yenlien.traceyun.business.service.rest.usercenter.DepartmentRestService;
import com.yenlien.traceyun.business.service.rest.usercenter.UserRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-11-24
 * @description:
 **/

@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController {

    @Value("${traceyun.app.id}")
    private String appId;

    @Autowired
    private DepartmentRestService departmentRestService;

    @Autowired
    private UserRestService userRestService;

    @Autowired
    private UserService userService;

    @Autowired
    private ClientDao clientDao;

    @Autowired
    private SupplierDao supplierDao;

    /**
     * 导出用户模板
     *
     * @param response
     */
    @RequestMapping("/toUser")
    public void toUser(HttpServletResponse response, HttpServletRequest req) {
        String companyId = req.getParameter("companyId");
        JSONArray array = userService.outTemplate(companyId);
        userService.file(response, array);
    }

    /**
     * 导入用户模板
     *
     * @param request
     */
    @RequestMapping("/importUser")
    public Object importUser(HttpServletRequest request) {
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        List<UserBusiness> list = userService.parseExcel(file, companyId);
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(list));
        userRestService.importUser(array);
        return success("success");
    }

    /**
     * 导入客户模板
     *
     * @param request
     */
    @RequestMapping("/importClient")
    public Object importClient(HttpServletRequest request) {
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        List<Client> clients = userService.clientParseExcel(file, companyId);
        clientDao.saveAll(clients);
        return success("success");
    }

    /**
     * 导出客户模板
     *
     * @param response
     */
    @RequestMapping("/toClient")
    public Object toClient(HttpServletResponse response) {
        userService.fileClient(response);
        return success("success");
    }

    /**
     * 供应商导入模板
     *
     * @param request
     */
    @RequestMapping("/importSupplier")
    public Object importSupplier(HttpServletRequest request) {
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        List<Supplier> suppliers = userService.supplierParseExcel(file, companyId);
        supplierDao.saveAll(suppliers);
        return success("success");
    }

    /**
     * 供应商模板导出
     *
     * @param response
     * @return
     */
    @RequestMapping("/toSupplier")
    public Object toSupplier(HttpServletResponse response) {
        userService.fileSupplier(response);
        return success("success");
    }

    /**
     * 工艺工序模板导出
     *
     * @param response
     * @return
     */
    @RequestMapping("/toCraft")
    public void toCraft(HttpServletResponse response, HttpServletRequest req) {
        userService.toCraft(response, userService.toCraftDataV(req));
    }

    /**
     * 工艺工序导入
     *
     * @param req
     */
    @RequestMapping("/importCraft")
    public Object importCraft(HttpServletRequest req) {
        try {
            userService.importCraft(((MultipartHttpServletRequest) req).getFile("file"), UserAuthUtil.getLoginUser().getCompanyId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success("success");
    }


}
